<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$ui = new ui();
$tpl = new Template2();
$fieldArr = array( "method" => "5", "gametype" => "0", "round" => "0", "lotto_id" => "0", "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "2", "paperopen" => date( "Y-m-d" ) );
$tpl->load( "main", "srate3.htm" );
$disk = "a";
if ( isset( $_GET['disk'] ) )
{
    $disk = $_GET['disk'];
}
else if ( isset( $_POST['disk'] ) )
{
    $disk = $_POST['disk'];
}
include( "admin_menu.php" );
include( "rate_menu.php" );
srateMenu( "srate3", $disk );
$tpl->set( "action", "op.php?op=admin&fp=srate3&disk=".$disk );
if ( $disk == "a" )
{
    $setRateFlag = true;
}
else
{
    $setRateFlag = false;
}
if ( ispost( ) )
{
    $fields = array();
    if ( $setRateFlag )
    {
        $sqlRate = "!!SELECT * FROM rate1a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb( $sqlRate );
    }
    $i = 1;
    while ( $i <= 12 )
    {
        $temp = floatval( $_POST["r21_".$i] );
        if ( $temp < 10 )
        {
            $fields["r21_".$i] = $temp;
        }
        ++$i;
    }
    $i = 1;
    while ( $i <= 10 )
    {
        $temp = floatval( $_POST["r22_".$i] );
        if ( $temp < 10 )
        {
            $fields["r22_".$i] = $temp;
        }
        ++$i;
    }
    
    $sql = "update rate1".$disk." set ";
    foreach ( $fields as $key => $temp )
    {
        $sql .= " {$key} = {$temp}, ";
        if ( $setRateFlag && !$rsRate->eof && $rsRate->f[$key] != $temp )
        {
            $fieldArr['betcontent'] = GetBetContentForSetRateRecord( $key );
            $fieldArr['orate'] = $rsRate->f[$key];
            $fieldArr['nrate'] = $temp;
            setRateRecord( $fieldArr );
        }
    }
    if ( substr( $sql, 0 - 2 ) == ", " )
    {
        $sql = substr( $sql, 0, strlen( $sql ) - 2 );
    }
    $sql .= " WHERE lotto_id=0";
    getdb( $sql );
}
$rs = getdb( "!!select * from rate1".$disk." where lotto_id=0" );
if ( $rs->eof )
{
    getdb( "insert into rate1".$disk." (lotto_id) values (0)" );
    $rs = getdb( "!!select * from rate1".$disk." where lotto_id=0" );
}
$i = 1;
while ( $i <= 12 )
{
    $tpl->set( "r21_".$i, trimfloat( $rs->f["r21_".$i], 1 ) );
    ++$i;
}
$i = 1;
while ( $i <= 10 )
{
    $tpl->set( "r22_".$i, trimfloat( $rs->f["r22_".$i], 1 ) );
    ++$i;
}
$tpl->show( "main" );
?>